<?php defined('SYSPATH') or die('No direct script access.');
/**
* Module Products Detail
*/
class Module_Products_ProductsDetail {
	
	private $controller;
	private $ss;
	private $thesystems;
	private $myfunctions;
	private $mydatetime;
	private $message;
	private $arraylist;
	
	static function &factory() {
	   $o = new Module_Products_ProductsDetail();
	  return $o;	
	}
	
	public function __construct()
	{
		$this->controller  = Request::current()->controller();
		$this->ss          = Session::instance();
		$this->thesystems  = Helper_theSystems::factory();
		$this->myfunctions = Helper_myFunctions::factory();
		$this->mydatetime  = Helper_MyDateTime::factory();
		$this->message     = Language_Message::factory();
		$this->arraylist   = Helper_ArrayList::factory();
	}
	
	public function productsdetail_tabs($command, $products_id, $id)
	{
		switch ($command)
		{
		case "create":
				  // Create New Products Detail
				  $products_id		= $products_id;
				  $position				= $_POST['position'];
				  $userroles_key	= $_POST['userroles_key'];
				  $approve				= Arr::get($_POST,('approve'));
				  $datetime				= $this->mydatetime->save_to_sql_datetime_now('datetime');
					$menuTitle			= $_POST['menuTitle'];
				  $pageTitle			= $_POST['pageTitle'];
				  $content				= $_POST['content'.$this->ss->get('langid')];
				  
				  $query = DB::insert('proddetails', array('products_id','position','userroles_key','approve','datetime','menuTitle','pageTitle','content'.$this->ss->get('langid')))
					  ->values(array($products_id,$position,$userroles_key,$approve,$datetime,$menuTitle,$pageTitle,$content));
					  $result = $query->execute();
					if ($result[1] == 1) {
						$this->message->store_message('c_success');
					  $r = Request::current();
					  $r->redirect($this->controller.'/productsdetail?products_id='.$products_id.'&id='.$result[0]);
					} else {
						$this->message->store_message('c_fails');
					}
					break;
		
		case "update":
				  // Update Products Detail
				  $p['products_id']												= $products_id;
				  $p['position']													= $_POST['position'];
				  $p['userroles_key']											= $_POST['userroles_key'];
				  $p['approve']														= Arr::get($_POST,('approve'));
				  $p['menuTitle']													= $_POST['menuTitle'];
				  $p['pageTitle']													= $_POST['pageTitle'];
				  $p['content'.$this->ss->get('langid')]	= $_POST['content'.$this->ss->get('langid')];
				  
				  $query = DB::update('proddetails')
					  ->set($p)
					  ->where('id', '=', $id);
				  $result = $query->execute();
					  if ($result == 1) {
							$this->message->store_message('u_success');
							$r = Request::current();
							$r->redirect($this->controller.'/productsdetail?products_id='.$products_id.'&id='.$id);
					  } else {
							$this->message->store_message('u_fails');
					  }
					break;
		
		case "delete":
					// Delete Products Detail Data
				  $query = DB::delete('proddetails')
					  ->where('id', '=', $id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
							$this->message->store_message('d_success');
							$r = Request::current();
							$r->redirect($this->controller.'/productsdetail?products_id='.$products_id);
					  } else {
							$this->message->store_message('d_fails');
					  }
					break;
		}
		
		$products_detail = DB::select()
			->from('proddetails')
			->where('products_id','=',$products_id)
			->order_by('position', 'ASC')
			->order_by('menuTitle', 'ASC')
			->execute();
		$products_details = $products_detail->as_array();
		
		$products_detail_update = DB::select()
			->from('proddetails')
			->where('id', '=', $id)
			->limit(1)
			->execute();
		
		$content['title']		= 'Product Detail';
		$content['content']	= View::factory('posinc/products_detail_tabs')
			->set('products_details',$products_details)
			->set('products_detail_update',$products_detail_update[0])
			->render();
		
		return $content;
	}
	
	public function productsdetail_skus($command, $products_id, $id, $image_id)
	{
		switch ($command)
		{
		case "create":
				  // Create New Products Detail SKUs
				  $products_id		= $products_id;
					$prodoptions_id	= $_POST['prodoptions_id'];
					$code						= $_POST['code'];
					$accstores_id		= NULL;
					$value					= $_POST['value'];
					$date						= $_POST['date'];
					$price01				= $_POST['price01'];
					$price02				= $_POST['price02'];
					$price03				= $_POST['price03'];
					$price04				= $_POST['price04'];
					$weight					= $_POST['weight'];
					$width					= $_POST['width'];
					$length					= $_POST['length'];
					$depth					= $_POST['depth'];
					$stock					= $_POST['stock'];
				  $approve				= Arr::get($_POST,('approve'));
					
				  $query = DB::insert('prodskus', array('products_id','prodoptions_id','accstores_id','code','value','date','price01','price02','price03','price04','weight','width','length','depth','stock','approve'))
					  ->values(array($products_id,$prodoptions_id,$accstores_id,$code,$value,$date,$price01,$price02,$price03,$price04,$weight,$width,$length,$depth,$stock,$approve));
					  $result = $query->execute();
					if ($result[1] == 1) {
						$this->message->store_message('c_success');
					  $r = Request::current();
					  $r->redirect($this->controller.'/productsdetail?products_id='.$products_id.'&id='.$result[0]);
					} else {
						$this->message->store_message('c_fails');
					}
					break;
		
		case "update":
				  // Update Products Detail SKUs
				  $p['products_id']			= $products_id;
				  $p['prodoptions_id']	= $_POST['prodoptions_id'];
				  $p['accstores_id']		= NULL;
				  $p['code']						= $_POST['code'];
				  $p['value']						= $_POST['value'];
				  $p['date']						= $_POST['date'];
				  $p['price01']					= $_POST['price01'];
				  $p['price02']					= $_POST['price02'];
				  $p['price03']					= $_POST['price03'];
				  $p['price04']					= $_POST['price04'];
				  $p['weight']					= $_POST['weight'];
				  $p['width']						= $_POST['width'];
				  $p['length']					= $_POST['length'];
				  $p['depth']						= $_POST['depth'];
				  $p['stock']						= $_POST['stock'];
				  $p['approve']					= Arr::get($_POST,('approve'));
				  
				  $query = DB::update('prodskus')
					  ->set($p)
					  ->where('id', '=', $id);
				  $result = $query->execute();
					  if ($result == 1) {
							$this->message->store_message('u_success');
							$r = Request::current();
							$r->redirect($this->controller.'/productsdetail?products_id='.$products_id.'&id='.$id);
					  } else {
							$this->message->store_message('u_fails');
					  }
					break;
		
		case "delete":
				  // Delete Image Files
				  $deletefiles = DB::select()
					  ->from('prodimages')
					  ->where('productskus_id', '=', $id)
					  ->execute();
				  $deletefilesresults = $deletefiles->as_array();
				  
				  $dir = 'assets/global/products/';
				  
				  foreach($deletefilesresults as $deletefilesresult)
				  {
					  if (file_exists($dir.$this->myfunctions->set_filename($deletefilesresult['filename'],'large')))
					  {
						  unlink($dir.$this->myfunctions->set_filename($deletefilesresult['filename'],'large'));
					  }
					  if (file_exists($dir.$this->myfunctions->set_filename($deletefilesresult['filename'],'thumb')))
					  {
						  unlink($dir.$this->myfunctions->set_filename($deletefilesresult['filename'],'thumb'));
					  }
					  if (file_exists($dir.$this->myfunctions->set_filename($deletefilesresult['filename'],'small')))
					  {
						  unlink($dir.$this->myfunctions->set_filename($deletefilesresult['filename'],'small'));
					  }
					}
				  
				  // Delete Image Data
				  $query = DB::delete('prodimages')
					  ->where('productskus_id', '=', $id)
						->execute();
					
					// Delete Product Data
				  $query = DB::delete('prodskus')
					  ->where('id', '=', $id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
							$this->message->store_message('d_success');
							$r = Request::current();
							$r->redirect($this->controller.'/productsdetail?products_id='.$products_id);
					  } else {
							$this->message->store_message('d_fails');
					  }
					break;
		
		case "image_create":
				  // Create Image Data and Upload Image
				  $datetime	= $this->mydatetime->save_to_sql_datetime_now('datetime');
				  
				  // Upload Image
				  if($_FILES['filename']['error']==0)
				  {
					  $filename = Upload::save($_FILES['filename'],NULL,'assets/global/products/');
					  
						// Resize, and save the image as SMALL
					  Image::factory($filename)
						  ->resize(100, NULL)
						  ->save('assets/global/products/'.$this->myfunctions->set_filename(basename($filename),'small'));
						
					  // Check Width and Height of the Images
					  $imgheight	= Image::factory($filename)->height;
					  $imgwidth		= Image::factory($filename)->width;
					  
					  // Resize, and save the image (Check Landscape or Portrait)
					  if($imgheight < $imgwidth)
					  {
						  // For Landscape Photo
							// Resize, Crop, and save the image as THUMBNAIL
						  Image::factory($filename)
							  ->resize(220, NULL)
							  ->crop(200,200)
							  ->save('assets/global/products/'.$this->myfunctions->set_filename(basename($filename),'thumb'));
						  
							// Resize, and save the image as LARGE
							Image::factory($filename)
							  ->resize(600, NULL)
							  ->save('assets/global/products/'.$this->myfunctions->set_filename(basename($filename),'large'));
						  
					  } else {
						  // For Portrait Photo
							// Resize, Crop, and save the image as THUMBNAIL
						  Image::factory($filename)
							  ->resize(NULL, 220)
							  ->crop(200,200)
							  ->save('assets/global/products/'.$this->myfunctions->set_filename(basename($filename),'thumb'));
						  
							// Resize, and save the image as LARGE
							Image::factory($filename)
							  ->resize(NULL, 600)
							  ->save('assets/global/products/'.$this->myfunctions->set_filename(basename($filename),'large'));
					  }
				  } else {
						$this->message->store_message('upload_criteria_fails');
					  $r = Request::current();
					  $r->redirect($this->controller.'/productsdetail?products_id='.$products_id.'&id='.$id);
				  }
				  
				  // Insert Data
				  $query = DB::insert('prodimages', array('products_id','productskus_id','datetime','filename'))
				  ->values(array(NULL,$id,$datetime,basename($filename)));
				  $result = $query->execute();
					  if ($result[1] == 1)
					  {
						  unlink('assets/global/products/'.basename($filename));
							
							$this->message->store_message('c_success');
						  $r = Request::current();
						  $r->redirect($this->controller.'/productsdetail?products_id='.$products_id.'&id='.$id);
					  } else {
							$this->message->store_message('c_fails');
					  }
					break;
		
		case "image_delete":
				  // Delete Image Files
				  $deletefiles = DB::select()
					  ->from('prodimages')
					  ->where('id', '=', $image_id)
					  ->execute();
				  $deletefilesresults = $deletefiles->as_array();
				  
				  $dir = 'assets/global/products/';
				  
				  foreach($deletefilesresults as $deletefilesresult)
				  {
					  if (file_exists($dir.$this->myfunctions->set_filename($deletefilesresult['filename'],'large')))
					  {
						  unlink($dir.$this->myfunctions->set_filename($deletefilesresult['filename'],'large'));
					  }
					  if (file_exists($dir.$this->myfunctions->set_filename($deletefilesresult['filename'],'thumb')))
					  {
						  unlink($dir.$this->myfunctions->set_filename($deletefilesresult['filename'],'thumb'));
					  }
					  if (file_exists($dir.$this->myfunctions->set_filename($deletefilesresult['filename'],'small')))
					  {
						  unlink($dir.$this->myfunctions->set_filename($deletefilesresult['filename'],'small'));
					  }
					}
				  
				  // Delete Image Data
				  $query = DB::delete('prodimages')
					  ->where('id', '=', $image_id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
							$this->message->store_message('d_success');
						  $r = Request::current();
						  $r->redirect($this->controller.'/productsdetail?products_id='.$products_id.'&id='.$id);
					  } else {
							$this->message->store_message('d_fails');
					  }
					break;

		}
		
		$products_detail = DB::select()
			->from('prodskus')
			->where('products_id','=',$products_id)
			->order_by('value', 'ASC')
			->execute();
		$products_details = $products_detail->as_array();
		
		$products_detail_update = DB::select()
			->from('prodskus')
			->where('id', '=', $id)
			->limit(1)
			->execute();
		
		$content['title']		= 'Product Detail SKUs';
		$content['content']	= View::factory('inc/products_detail_skus')
			->set('products_details',$products_details)
			->set('products_detail_update',$products_detail_update[0])
			->render();
		
		return $content;
	}
	
	public function productsdetail_fifo($command, $products_id, $id, $image_id)
	{
		switch ($command)
		{
		case "create":
					$code    = $_POST['code'];
					$value   = $_POST['value'];
					$date    = $_POST['date'];
					$stock   = $_POST['stock'];
					$approve = Arr::get($_POST,('approve'));
					$products_id = Arr::get($_GET,'products_id');
					
				  $query = DB::insert('prodskus', array('code','value','date','stock','approve','products_id'))
					  ->values(array($code,$value,$date,$stock,$approve,$products_id));
					  $result = $query->execute();
					if ($result[1] == 1) {
						$this->message->store_message('c_success');
					  $r = Request::current();
					  $r->redirect($this->controller.'/productsdetail?products_id='.$products_id.'&id='.$result[0]);
					} else {
						$this->message->store_message('c_fails');
					}
				break;

		case "update":
		  // Update Products Detail SKUs
			$p['code']  = $_POST['code'];
			$p['value'] = $_POST['value'];
			$p['date']  = $_POST['date'];
			$p['stock'] = $_POST['stock'];
			$p['stock'] = Arr::get($_POST,('stock'));
		  
		  $query = DB::update('prodskus')
			  ->set($p)
			  ->where('id', '=', $id);
		  $result = $query->execute();
			  if ($result == 1) {
					$this->message->store_message('u_success');
					$r = Request::current();
					$r->redirect($this->controller.'/productsdetail?products_id='.$products_id.'&id='.$id);
			  } else {
					$this->message->store_message('u_fails');
			  }
				break;

		case "delete":
					// Delete Product Data
				  $query = DB::delete('prodskus')
					  ->where('id', '=', $id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
							$this->message->store_message('d_success');
							$r = Request::current();
							$r->redirect($this->controller.'/productsdetail?products_id='.$products_id);
					  } else {
							$this->message->store_message('d_fails');
					  }
					break;
		}
		
		$products_detail = DB::select()
			->from('prodskus')
			->where('products_id','=',$products_id)
			->order_by('id', 'ASC')
			->execute();
		$products_details = $products_detail->as_array();
		
		$products_detail_update = DB::select()
			->from('prodskus')
			->where('id', '=', $id)
			->limit(1)
			->execute();

		$content['title']		= 'Product Detail Fifo';
		$content['content']	= View::factory('inc/products_detail_fifo')
			->set('products_details',$products_details)
			->set('products_detail_update',$products_detail_update[0])
			->render();
		
		return $content;

	}	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}